package com.yilin.tms.truck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.model.AuthStatus;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.truck.TransportLicense;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.remote.IMessageRemote;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.truck.service.ITransportLicenseService;
import com.yilin.tms.truck.service.ITruckVehicleService;
import com.yilin.tms.truck.mapper.TransportLicenseMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;

/**
 * 道路运输证服务
 *
 * @author zhangjinyuan
 * @date 2022/11/11
 */
@Service
@Transactional
public class TransportLicenseService extends ServiceImpl<TransportLicenseMapper, TransportLicense> implements ITransportLicenseService {

    @Resource
    ITruckVehicleService vehicleService;

    @Resource
    IUserRemote userRemote;
    @Resource
    IMessageRemote messageRemote;

    /**
     * 获取运输证分页
     */
    @Override
    public PageData<TransportLicense> getTransportLicensePage(Account loginUser, TransportLicense query, Integer start, Integer limit) {
        QueryWrapper<TransportLicense> queryWrapper = new QueryWrapper<>();
        if (loginUser.getUserType() != UserType.platformUser&&loginUser.getUserType() != UserType.adminUser) {
            queryWrapper.eq("organize_id", loginUser.getOrganizeId());
            queryWrapper.eq("superior_plat_id", loginUser.getOrganizeId());
        }
        QueryUtil.andQueryWrapper(queryWrapper,query);
        return page(new PageData<>(start, limit), queryWrapper);
    }
    /**
     * 保存道路运输证
     */
    @Override
    public void saveTransportLicense(Account user, TransportLicense transportLicense) {
        ParamUtil.isNull(transportLicense.getVehicleId(), "车辆ID为空");
        ParamUtil.isNull(transportLicense.getVehicleNo(), "车牌号为空");
        ParamUtil.isNull(transportLicense.getTransLicensePhoto(), "图片为空");
        ParamUtil.isNull(transportLicense.getTransLicenseNo(), "司机运输证编号为空");
        ParamUtil.isNull(transportLicense.getTransLicenseName(), "运输证户名为空");
        transportLicense.setOrganizeId(user.getOrganizeId());
        transportLicense.setOrganizeName(user.getOrganizeName());
        if(user.getSuperiorPlatId()!=null){
            transportLicense.setSuperiorPlatId(user.getSuperiorPlatId());
            transportLicense.setSuperiorPlatName(user.getSuperiorPlatName());
        }
        transportLicense.setAuthStatus(AuthStatus.waitProcess);
        if (transportLicense.getId() == null) {
            transportLicense.setAddUserId(user.getId());
            transportLicense.setCreateTime(new Date());
            save(transportLicense);
        } else {
            transportLicense.setUpdateUserId(user.getId());
            transportLicense.setUpdateTime(new Date());
            updateById(transportLicense);
        }
    }

    /**
     * 审核并更新道路运输证数据
     */
    @Override
    public void verifyAndUpdateAuth(Account loginUser, TransportLicense transportLicense) {
        TruckVehicle truckVehicleById = vehicleService.getById(transportLicense.getVehicleId());
        Organize organize = userRemote.getOrganizeById(truckVehicleById.getOrganizeId()).getData();
        Account account = userRemote.getAccountById(organize.getManagerUserId()).getData();
        transportLicense.setAuthStatus(transportLicense.getAuthStatus());
        transportLicense.setFailedMsg(transportLicense.getFailedMsg());
        transportLicense.setAutTime(new Date());
        transportLicense.setAutUserId(loginUser.getId());
        transportLicense.setUpdateTime(new Date());
        transportLicense.setUpdateUserId(loginUser.getId());
        if (transportLicense.getAuthStatus() != AuthStatus.authSuccess) {
            String text = String.format("您的道路运输证认证失败，请尽快登录APP重新认证。失败原因:%s", transportLicense.getFailedMsg());
            ParamUtil.isSuccess(messageRemote.sendSystemNotify(account.getId(), text, null));
//            messageRemote.sendPhoneMessage(account.getPhone(), text);
        } else {
            TruckVehicle truckVehicle = vehicleService.getById(transportLicense.getVehicleId());
            truckVehicle.setTmsTruckDataState(TruckVehicle.TmsTruckDataState.creating);
            vehicleService.updateById(truckVehicle);
            String TransportLicenseText = "您的道路运输证认证成功了！您现在可以登录APP进行下一步操作~";
            ParamUtil.isSuccess(messageRemote.sendSystemNotify(account.getId(),TransportLicenseText, null));
//            messageRemote.sendPhoneMessage(account.getPhone(), TransportLicenseText);
        }
        updateById(transportLicense);
    }

}
